package dao;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Restrictions;

import entity.BSC_Perspective;
import entity.StrategicPerspective;
import entity.StrategicPlan;

import util.HibernateUtil;
import vo.BSCPerspectiveVO;
import vo.PerspectiveVO;

public class BSCPerspectiveDAO {

	public Map<String, PerspectiveVO> getPerspectives(int strategicPlanId) {
		Session sessao = HibernateUtil.getSession();		
		StrategicPlan sp = (StrategicPlan) sessao.load(StrategicPlan.class, strategicPlanId);
		
		Map<String, PerspectiveVO> retorno = new HashMap<String, PerspectiveVO>();
		for(StrategicPerspective spp: sp.getOrganizacao().getPerspectives()){			
			PerspectiveVO vo = new PerspectiveVO();
			vo.setId(spp.getId());
			vo.setNome(spp.getName());
			vo.setAcronym(spp.getAcronym());
			
			retorno.put(Integer.toString(spp.getId()), vo);
		}		
		
		sessao.close();
		return retorno;
	}

	@SuppressWarnings("unchecked")
	public Map<String, BSCPerspectiveVO> getUsedPerspectives(int strategicPlanId) {
		Session sessao = HibernateUtil.getSession();
		Criteria criterios = sessao.createCriteria(BSC_Perspective.class);
		criterios.add(Restrictions.eq("strategicPlan.id", strategicPlanId));
		List<BSC_Perspective> perspectives = (List<BSC_Perspective>)criterios.list();
		
		Map<String, BSCPerspectiveVO> retorno = new HashMap<String, BSCPerspectiveVO>();
		for(BSC_Perspective bsc:perspectives){
			BSCPerspectiveVO vo = new BSCPerspectiveVO();
			vo.setPerspectiveId(bsc.getStrategicPerspective().getId());
			vo.setStrategicPlanId(strategicPlanId);
			vo.setPerspective(bsc.getStrategicPerspective().getName());
			vo.setId(bsc.getId());
			vo.setPosition(bsc.getPosition());
			vo.setAcronym(bsc.getStrategicPerspective().getAcronym());
					
			retorno.put(Integer.toString(bsc.getStrategicPerspective().getId()), vo);
		}
		
		sessao.close();
		return retorno;
	}

	public void save(List<String> listaUsedPerspectives, int strategicPlanId) {
		Session sessao = HibernateUtil.getSession();
		Transaction tx = sessao.beginTransaction();
		
		try{
			StrategicPlan sp = (StrategicPlan) sessao.load(StrategicPlan.class, strategicPlanId);
			Map<String, BSCPerspectiveVO> mapaUsedPerspective = getUsedPerspectives(strategicPlanId);
			
			if (sp.getListPerspectives() == null){
				sp.setListPerspectives(new ArrayList<BSC_Perspective>());
			}
			
			int i = 0;
			for(String key:listaUsedPerspectives){
				StrategicPerspective spp = (StrategicPerspective) sessao.load(StrategicPerspective.class, Integer.parseInt(key));
				BSC_Perspective bscp = new BSC_Perspective();
				
				if (mapaUsedPerspective.containsKey(key)){
					 mapaUsedPerspective.remove(key);
					 Criteria criterios = sessao.createCriteria(BSC_Perspective.class);					 
					 criterios.add(Restrictions.eq("strategicPlan.id", sp.getId()));
					 criterios.add(Restrictions.eq("strategicPerspective.id", spp.getId()));
					 bscp = (BSC_Perspective) criterios.list().get(0);
				}
				
				bscp.setStrategicPerspective(spp);
				bscp.setStrategicPlan(sp);
				bscp.setPosition(i);
				sessao.saveOrUpdate(bscp);
				
				i++;
			}
			
			for(String key:mapaUsedPerspective.keySet()){
				BSCPerspectiveVO vo = mapaUsedPerspective.get(key);				
				BSC_Perspective bscp = (BSC_Perspective) sessao.load(BSC_Perspective.class, vo.getId());
				sessao.delete(bscp);
			}
			
			tx.commit();
			sessao.close();
		}
		catch(Exception e){
			e.printStackTrace();
			tx.rollback();
			sessao.close();
		}
	}
}
